//
//  MonthlyPaymentCell.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2023/10/20.
//

import UIKit

class MonthlyPaymentCell: UITableViewCell {

    var bgView: UIView!
    var bgLayer1:CALayer!
    
    var prepaidAmountLab: UILabel!
    var questionBtn: UIButton!
    var prepaidPriceLab: UILabel!
    
    var monthlyLab: UILabel!
    var monthlyPriceLab: UILabel!
    var model: CarDetailsContentModel!
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        self.backgroundColor = .white
        self.clipsToBounds = true
        self.initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initUI(){
        
        
        self.bgView = UIView(frame: .zero)
        self.bgView.backgroundColor = .white
        self.bgView.layer.cornerRadius = 8
        self.bgView.clipsToBounds = false
        
        bgLayer1 = CALayer()
        bgLayer1.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1).cgColor
        self.bgView.layer.addSublayer(bgLayer1)
        // shadowCode
        self.bgView.layer.shadowColor = UIColor(red: 0.96, green: 0.96, blue: 0.98, alpha: 0.9).cgColor
        self.bgView.layer.shadowOffset = CGSize(width: 0, height: 0)
        self.bgView.layer.shadowOpacity = 1
        self.bgView.layer.shadowRadius = 10
        self.contentView.addSubview(bgView)

      
        self.prepaidAmountLab = UILabel(frame: CGRect(x: 18, y: 0, width: 100, height: 52))
        self.prepaidAmountLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 12)
        self.prepaidAmountLab.textColor = Theme.titleLabColor
        self.bgView.addSubview(self.prepaidAmountLab)
        
        
        self.questionBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 14, height: 14))
        self.questionBtn.setImage(UIImage(named: "icon_question_mark"), for: .normal)
        self.questionBtn.addTarget(self, action: #selector(questionBtnAntion), for: .touchUpInside)
        self.questionBtn.contentHorizontalAlignment = .left
        self.bgView.addSubview(self.questionBtn)
        
        
        self.prepaidPriceLab = UILabel(frame: CGRect(x: 18, y: 0, width: 100, height: 52))
        self.prepaidPriceLab.font = UIFont.init(name: Theme.PingFangSCMedium, size: 20)
        self.prepaidPriceLab.textColor = Theme.mainColor
        self.bgView.addSubview(self.prepaidPriceLab)
        
        
        
        
        self.monthlyLab = UILabel(frame: .zero)
        self.monthlyLab.textColor = Theme.titleLabColor
        self.monthlyLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 12)
        self.monthlyLab.textAlignment = .left
        self.bgView.addSubview(self.monthlyLab)
        
        
        self.monthlyPriceLab = UILabel(frame: CGRect(x: 18, y: 0, width: 100, height: 52))
        self.monthlyPriceLab.font = UIFont.init(name: Theme.PingFangSCMedium, size: 20)
        self.monthlyPriceLab.textColor = Theme.mainColor
        self.bgView.addSubview(self.monthlyPriceLab)
        
        

        self.prepaidPriceLab.text = "HK$ 180,000.66"
        self.monthlyLab.text = MMLocalizedString("monthly_installment", comment: "月供")
        self.monthlyPriceLab.text = "HK$ 180,000.66"
        
        self.addLayout()
        
    }
    
    
    
    func addLayout(){
        
        self.bgView.snp.makeConstraints { make in
            make.top.equalTo(10)
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.height.equalTo(142)
            make.bottom.equalTo(-10)
        }
        bgLayer1.frame = bgView.bounds
        
        
        self.prepaidAmountLab.snp.makeConstraints { make in
            make.top.equalTo(20)
            make.left.equalTo(20)
            make.height.equalTo(30)
        }
        
        
        self.questionBtn.snp.makeConstraints { make in
            make.top.equalTo(20)
            make.left.equalTo(self.prepaidAmountLab.snp.right).offset(4)
            make.width.equalTo(30)
            make.height.equalTo(30)
        }
        
        self.prepaidPriceLab.snp.makeConstraints { make in
            make.top.equalTo(self.prepaidAmountLab.snp.bottom).offset(-2)
            make.left.equalTo(20)
            make.height.equalTo(28)
    
        }
        
        self.monthlyLab.snp.makeConstraints { make in
            make.top.equalTo(self.prepaidPriceLab.snp.bottom).offset(8)
            make.left.equalTo(20)
            make.right.equalTo(-14)
            make.height.equalTo(20)
        }
        
        
        self.monthlyPriceLab.snp.makeConstraints { make in
            make.top.equalTo(self.monthlyLab.snp.bottom).offset(-2)
            make.left.equalTo(20)
            make.right.equalTo(-14)
            make.height.equalTo(28)
        }
        
    }
    
    //MARk: - Antion
    @objc func questionBtnAntion() {
        
        let parantView = self.viewController().view
        guard let toPoint = parantView?.convert(self.questionBtn.center, fromViewOrWindow: self) else { return  }
        let overView = WBPopOverView.init(origin: toPoint, width: 250, height: 89, direction: WBArrowDirection.down1)
        overView?.backView.backgroundColor = UIColor(hex: 0x222222)
        overView?.backView.layer.cornerRadius = 8
        overView?.backView.layer.masksToBounds = true
        
        let contentLab = UILabel(frame: CGRect(x: 14, y: 0, width: overView!.width - 31, height: overView!.height))
        contentLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 12)
        contentLab.textColor = .white
        contentLab.numberOfLines = 0
        contentLab.text =  self.model?.reminder
        
        overView?.backView.addSubview(contentLab)
        overView?.pop()
    }
    
    //setModel
    func setModel(model: CarDetailsContentModel){
        self.model = model
        self.prepaidAmountLab.text = model.wayName
        let firsPrice = floor((model.firstAmount ?? 0) * 100)/100
        let firstAmount = XTManageCenter.showPriceInComma(source: (firsPrice).cleanZero)
        self.prepaidPriceLab.text = XTManageCenter.currentSign() + firstAmount
        
        //月供金額
        let monthePrice = floor((model.monthlyAmount ?? 0) * 100)/100//保留两位小数
        let monthlyAmount = XTManageCenter.showPriceInComma(source: (monthePrice).cleanZero)
        self.monthlyPriceLab.text = XTManageCenter.currentSign() + monthlyAmount
        
        self.updateConstraints()
    }
    
  
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
}
